package com.glink.manage.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glink.manage.domain.AttachmentImageBean;
import com.glink.manage.domain.PersonCertBean;
import com.glink.manage.mapper.PersonCertMapper;
import com.glink.manage.service.AttachmentService;
import com.glink.manage.service.PersonCertService;
import com.glink.manage.vo.person.PersonCertInfoVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author  qiushaoshan
 * @since 2025-01-13
 */
@Service
public class PersonCertServiceImpl extends ServiceImpl<PersonCertMapper, PersonCertBean> implements PersonCertService {

    @Autowired
    private AttachmentService attachmentService;
    
    /**
     * 根据人员信息删除资质证书
     *
     * @param personObjId 人员信息
     * @return 资质证书
     */
    @Override
    @Transactional(value = "transactionManager", propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean delByPersonId(String personObjId) {
        Integer count = this.lambdaQuery().eq(PersonCertBean::getPersonObjId, personObjId).count();
        if(Objects.nonNull(count) && count > 0){
            return this.lambdaUpdate().eq(PersonCertBean::getPersonObjId, personObjId).remove();
        }else{
            return true;
        }
    }

    /**
     * 根据人员信息查询资质证书
     *
     * @param personObjId 人员信息
     * @return 资质证书
     */
    @Override
    public List<PersonCertInfoVO> findVOByPersonObjId(String personObjId) {
        List<PersonCertBean> personCertBeanList = this.lambdaQuery().eq(PersonCertBean::getPersonObjId, personObjId).list();
        List<PersonCertInfoVO> certInfoVOList = Lists.newArrayList();
        
        if(CollectionUtils.isNotEmpty(personCertBeanList)){
            personCertBeanList.forEach(personCertBean -> {
                PersonCertInfoVO certInfoVO = new PersonCertInfoVO();
                certInfoVO.setType(personCertBean.getType());
                certInfoVO.setIfEnable(personCertBean.getIfEnable());
                certInfoVO.setFilePath(personCertBean.getFilePath());
                
                if(StringUtils.isNotBlank(personCertBean.getFilePath())){
                    List<String> list = Arrays.stream(personCertBean.getFilePath().split(",", -1)).collect(Collectors.toList());
                    List<AttachmentImageBean> attachmentImageBeanList = attachmentService.lambdaQuery().in(AttachmentImageBean::getId, list).list();
                    if(CollectionUtils.isNotEmpty(attachmentImageBeanList)){
                        Map<String, String> imageMap = attachmentImageBeanList.stream().collect(Collectors.toMap(AttachmentImageBean::getId, AttachmentImageBean::getName));
                        certInfoVO.setFileName(String.join(",", imageMap.values()));
                    }
                }
                
                certInfoVOList.add(certInfoVO);
            });
        }
        
        return certInfoVOList;
    }
}
